package com.example.a2022application;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.bean.UserInfoBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class ForgotpwdActivity extends BaseActivity {
    MySqliteHelper helper;
    private EditText etAccount;//账号
    private EditText etName; //名字
    private EditText etPassword;//密码
    private EditText etPasswordSure; //确认密码
    private TextView tvLogin; //返回登录
    private Button btnSubmit;//提交
    private Spinner spIdentity;//身份
    private String   getContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgotpwd);
        helper = new MySqliteHelper(this);
        etAccount =(EditText) findViewById(R.id.et_account);//获取账号
        etName =(EditText) findViewById(R.id.et_nickName);//获取名字
        etPassword=(EditText) findViewById(R.id.et_password);//获取密码
        etPasswordSure=(EditText) findViewById(R.id.et_password_sure);//获取确认密码
        tvLogin=(TextView) findViewById(R.id.tv_login);//登录
        btnSubmit=(Button) findViewById(R.id.btn_submit);//获取注册按钮
        spIdentity = findViewById(R.id.spin_identity);
        spIdentity.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                getContent = ForgotpwdActivity.this.getResources().getStringArray(R.array.professionals)[arg2];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        tvLogin.setOnClickListener(new View.OnClickListener() {
            //点击返回登陆
            @Override
            public void onClick(View v) {
                //跳转到登录页面
                Intent intent=new Intent(ForgotpwdActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //设置注册点击按钮
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取请求参数
                String account= etAccount.getText().toString();
                String nickName= etName.getText().toString();
                String password=etPassword.getText().toString();
                String passwordSure=etPasswordSure.getText().toString();


                if ("".equals(account)){//账号不能为空
                    Toast.makeText(ForgotpwdActivity.this,"账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("".equals(nickName)){//昵称不能为空
                    Toast.makeText(ForgotpwdActivity.this,"昵称不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("".equals(password)){//密码为空
                    Toast.makeText(ForgotpwdActivity.this,"密码为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!password.equals(passwordSure)){//密码不一致
                    Toast.makeText(ForgotpwdActivity.this,"密码不一致", Toast.LENGTH_LONG).show();
                    return;
                }
                OkGo.<String>post(Urls.DETAIL)
                        .params("account",account)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                UserInfoBean bean = getGson().fromJson(response.body(),UserInfoBean.class);
                                if (bean.getCode() == 0){
                                    if (!bean.getData().getNickname().equals(nickName)){
                                        ToastUtil.showToast("昵称输入错误");
                                    }else if (!bean.getData().getSpIdentity().equals(getContent)){
                                        ToastUtil.showToast("身份选择错误");
                                    }else {
                                       OkGo.<String>post(Urls.UPDATEPWD)
                                               .params("userid",bean.getData().getObjId())
                                               .params("password",password)
                                               .execute(new StringCallback() {
                                                   @Override
                                                   public void onSuccess(Response<String> response) {
                                                       RegisterBean bean1 = getGson().fromJson(response.body(),RegisterBean.class);
                                                       if (bean1.getCode() == 0){
                                                           finish();
                                                       }
                                                       ToastUtil.showToast(bean1.getMessage());
                                                   }
                                               });
                                    }
                                }else {
                                    ToastUtil.showToast(bean.getMessage());
                                }
                            }
                        });


            }
        });
    }
    public void back(View view){
        finish();
    }

}
